package no.ntnu.fp.mmi;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

import no.ntnu.fp.su.Watch;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/


@SuppressWarnings("serial")
public class MessagePanel extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel jLabel1;
	private JButton closeButton;
//	private JScrollBar jScrollBar1;
	private JTextArea jTextArea1;
	private JScrollPane jScrollPane1;
	private JButton sendButton;
	private WatchModel model;
//	private Watch watch;
//	private Message message;

	
	Font font = new Font("Candera", Font.BOLD, 17);
	/**
	* Auto-generated main method to display this JFrame
	*/

	
	/**Constructor <br>
	 * A Panel for creating and sending a message to master
	 */
	public MessagePanel(WatchModel model) {
		super();
		this.model = model;

			try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setFont(font);
				jLabel1.setText("Message To Master");
				jLabel1.setBounds(107, 12, 206, 28);
			}
			{
				closeButton = new JButton();
				getContentPane().add(closeButton);
				closeButton.setText("Close");
				closeButton.setBounds(97, 218, 83, 23);
				closeButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Object[] option = {"Yes", "No"};
						int val = javax.swing.JOptionPane.showOptionDialog(null, "Are you sure you want to close without saving?",
				                "Cancel?", javax.swing.JOptionPane.YES_NO_OPTION,
				                javax.swing.JOptionPane.QUESTION_MESSAGE, null, option, option[0]);
				        
				        if(val == 0) {
				            dispose();						
					}
					}
				});
				
			}
			{
				sendButton = new JButton();
				getContentPane().add(sendButton);
				sendButton.setText("Send");
				sendButton.setBounds(199, 218, 90, 23);
				sendButton.addActionListener(new sendMessageEvent());
			}
			{
				jTextArea1 = new JTextArea();
				getContentPane().add(getJTextArea1());
				jTextArea1.setForeground(Color.white);
				jTextArea1.setBackground(Color.lightGray);
				jTextArea1.setBounds(40, 52, 315, 139);
				{
					jScrollPane1 = new JScrollPane();
					jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
					jTextArea1.add(jScrollPane1);
			
				}

			}
			
		
			
			

		
			setSize(400, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public JTextArea getJTextArea1() {
		return jTextArea1;
	}
	/**
	 * Listener for send button, sends, closes and lets the user know message was sent
	 * @author Jaspreet
	 *
	 */
	public class sendMessageEvent implements ActionListener {
         public void actionPerformed(ActionEvent arg0) {
				boolean sent = model.getWatch().sendMessage("message" + Watch.SEPARATOR + model.getPlatform().getID() + Watch.SEPARATOR + (new Timestamp(System.currentTimeMillis())) + Watch.SEPARATOR +jTextArea1.getText());
				if (sent) {
					JOptionPane.showMessageDialog(null	, "Message Sent", "Message Sent", JOptionPane.INFORMATION_MESSAGE);
				dispose();
				}	
				else{
					JOptionPane.showMessageDialog(null	, "Message could not be sent", "Error", JOptionPane.INFORMATION_MESSAGE);

				}
				}
		}
	}



